<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="100px" style="width: 500px">

      <el-form-item label="工厂id" prop="id">
        <el-input v-model="ruleForm.id" readonly></el-input>
      </el-form-item>

      <el-form-item label="工厂名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="工厂状态" prop="tatusid" >
        <el-tag
            :type="ruleForm.statusid == 2 ? 'danger' : 'success'"
            disable-transitions>
          <span v-for="status in tages" v-show="status.value==ruleForm.statusid">
            {{status.text}}
          </span>
        </el-tag>
      </el-form-item>

      <el-form-item label="工厂简介" prop="description">
        <el-input v-model="ruleForm.description" type="textarea"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        id:null,
        name:null,
        description:null,
        managerid:null,
        statusid:null
      },
      rules: {
        name: [
          { required: true, message: '工厂名不能为空', trigger: 'blur' }
        ],
        description: [
          {max:200,message:'最多不超过200个字符',trigger: 'change'}
        ],
      },
      tages: [{text: '开启', value: 1}, {text: '关闭', value: 2}],
    };
  },created() {
    axios.get("http://localhost:8081/factory/findById/"+this.$store.state.factoryid).then((response)=>{
      this.ruleForm = response.data;
    })
  },
  methods: {
    changestatus(statusid) {
      axios.get('http://localhost:8081/factory/changeStatus/+' + this.$store.state.factoryid + '/' + statusid).then(
          (response) => {
            if (response.data != null) {
              this.success();
              axios.get("http://localhost:8081/factory/findById/"+this.$store.state.factoryid).then((response)=>{
                this.ruleForm = response.data;
              })
            }
          }
      )
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("http://localhost:8081/factory/update",this.ruleForm).then((response)=>{
            if(response.data == "success"){
              this.success();
            }else{
              this.failed();
            }
          })
        } else {
          this.failed();
          return false;
        }
      });
    },
    resetForm(formName) {
      axios.get("http://localhost:8081/factory/findById/"+this.$store.state.factoryid).then((response)=>{
        this.ruleForm = response.data;
      })
    },
    success() {
      this.$message({
        message: '操作成功!',
        type: 'success'
      });
    },
    failed() {
      this.$message.error('操作失败，请检查内容是否正确或稍后再试!');
    }
  }
}
</script>